﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;
    using System.Linq;

    public partial class frmCapNhatDmVatTu : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmVatTuEvent;
        int State = 0;
        DmVatTuEntity objDmVatTuEntity = null;

        DmVatTuBusiness dmVatTuBusiness = new DmVatTuBusiness();
        DmHangSanXuatBusiness dmHangSanXuatBusiness = new DmHangSanXuatBusiness();
        DmNhomVatTuBusiness dmNhomVatTuBusiness = new DmNhomVatTuBusiness();
        DmPhanLoaiVatTuBusiness dmPhanLoaiVatTuBusiness = new DmPhanLoaiVatTuBusiness();
        DmCachDungBusiness dmCachDungBusiness = new DmCachDungBusiness();
        DmDonViBusiness dmDonViBusiness = new DmDonViBusiness();
        DmGhiChuCachDungBussiness dmGhiChuCachDungBussiness = new DmGhiChuCachDungBussiness();
        DmLoaiVatTuBusiness dmLoaiVatTuBusiness = new DmLoaiVatTuBusiness();

        public frmCapNhatDmVatTu(int _status)
        {
            InitializeComponent();
            LoadDanhMuc();

            State = _status;
        }
        public frmCapNhatDmVatTu(int _status, DmVatTuEntity obj)
        {
            InitializeComponent();
            LoadDanhMuc();

            txtMaVatTu.Enabled = false;
            State = _status;
            objDmVatTuEntity = obj;

            txtMaVatTu.Text = objDmVatTuEntity.MaVatTu;
            txtTenVatTu.Text = objDmVatTuEntity.TenVatTu;
            txtTenGoc.Text = objDmVatTuEntity.TenGoc;
            lkDonVi.EditValue = objDmVatTuEntity.MaDonVi;
            lkHangSanXuat.EditValue = objDmVatTuEntity.MaHangSanXuat;
            lkLoaiVatTu.EditValue = objDmVatTuEntity.MaLoaiVatTu;
            lkPhanLoaiVatTu.EditValue = objDmVatTuEntity.MaPhanLoaiVatTu;
            txtThanhPhan.Text = objDmVatTuEntity.ThanhPhan;
            txtHamLuong.Text = objDmVatTuEntity.HamLuong;
            chkTrangThaiSuDung.Checked = objDmVatTuEntity.TrangThaiSuDung;

            lkCachDungThuoc.EditValue = objDmVatTuEntity.MaCachDung;
            txtSoLanDungThuoc.Text = objDmVatTuEntity.LanDung.ToString();
            txtSoLuongDungThuoc.Text = objDmVatTuEntity.SoLuongDung.ToString();
            lkDonViDungThuoc.EditValue = objDmVatTuEntity.MaDonViDung;
            lkGhiChuCachDung.EditValue = objDmVatTuEntity.MaGhiChuCachDung;
            txtHuongDanSuDungThuoc.Text = objDmVatTuEntity.HuongDanSuDung;

            txtTenVatTu.Focus();
        }

        #region "Load Danh Muc"
        void LoadDmHangSx()
        {
            var lstHangSanXuatEntity = dmHangSanXuatBusiness.GetDmHangSanXuatList();
            lstHangSanXuatEntity.Insert(0, new DmHangSanXuatEntity() { MaHang = ConstantManagerClinic.sValueMember, TenHang = ConstantManagerClinic.sDisplayMember });
            lkHangSanXuat.Properties.DataSource = lstHangSanXuatEntity;
            lkHangSanXuat.Properties.DisplayMember = "TenHang";
            lkHangSanXuat.Properties.ValueMember = "MaHang";
            lkHangSanXuat.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadLoaiVatTu()
        {
            var lstLoaiVatTuEntity = dmLoaiVatTuBusiness.GetDmLoaiVatTuList();
            lstLoaiVatTuEntity.Insert(0, new DmLoaiVatTuEntity() { MaLoaiVatTu = ConstantManagerClinic.sValueMember, TenLoaiVatTu = ConstantManagerClinic.sDisplayMember });
            lkLoaiVatTu.Properties.DataSource = lstLoaiVatTuEntity;
            lkLoaiVatTu.Properties.ValueMember = "MaLoaiVatTu";
            lkLoaiVatTu.Properties.DisplayMember = "TenLoaiVatTu";
            lkLoaiVatTu.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadPhanLoaiVt()
        {
            var lstPhanLoaiVatTuEntity = dmPhanLoaiVatTuBusiness.GetDmPhanLoaiVatTuList(lkLoaiVatTu.EditValue.ToString());
            lstPhanLoaiVatTuEntity.Insert(0, new DmPhanLoaiVatTuEntity() { MaPhanLoaiVatTu = ConstantManagerClinic.sValueMember, TenPhanLoaiVatTu = ConstantManagerClinic.sDisplayMember });

            lkPhanLoaiVatTu.Properties.DataSource = lstPhanLoaiVatTuEntity;
            lkPhanLoaiVatTu.Properties.ValueMember = "MaPhanLoaiVatTu";
            lkPhanLoaiVatTu.Properties.DisplayMember = "TenPhanLoaiVatTu";
            lkPhanLoaiVatTu.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmCachDung()
        {
            var lstDmCachDungEntity = dmCachDungBusiness.GetDmCachDungList();
            lstDmCachDungEntity.Insert(0, new DmCachDungEntity() { MaCachDung = ConstantManagerClinic.sValueMember, TenCachDung = ConstantManagerClinic.sDisplayMember });
            lkCachDungThuoc.Properties.DataSource = lstDmCachDungEntity;
            lkCachDungThuoc.Properties.DisplayMember = "TenCachDung";
            lkCachDungThuoc.Properties.ValueMember = "MaCachDung";
            lkCachDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmDonVi()
        {
            var lstDmDonViEntiy = dmDonViBusiness.GetDmDonViList();
            lstDmDonViEntiy.Insert(0, new DmDonViEntity() { MaDonVi = ConstantManagerClinic.sValueMember, TenDonVi = ConstantManagerClinic.sDisplayMember });
            lkDonVi.Properties.DataSource = lstDmDonViEntiy;
            lkDonVi.Properties.DisplayMember = "TenDonVi";
            lkDonVi.Properties.ValueMember = "MaDonVi";
            lkDonVi.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmDonViCachDung()
        {
            var lstDmDonViEntiy = dmDonViBusiness.GetDmDonViList();
            lstDmDonViEntiy.Insert(0, new DmDonViEntity() { MaDonVi = ConstantManagerClinic.sValueMember, TenDonVi = ConstantManagerClinic.sDisplayMember });
            lkDonViDungThuoc.Properties.DataSource = lstDmDonViEntiy;
            lkDonViDungThuoc.Properties.DisplayMember = "TenDonVi";
            lkDonViDungThuoc.Properties.ValueMember = "MaDonVi";
            lkDonViDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmGhiChuCachDung()
        {
            var lstGhiChuCachDungEntity = dmGhiChuCachDungBussiness.GetDmGhiChuCachDungList();
            lstGhiChuCachDungEntity.Insert(0, new DmGhiChuCachDungEntity() { MaGhiChuCachDung = ConstantManagerClinic.sValueMember, TenGhiChuCachDung = ConstantManagerClinic.sDisplayMember });
            lkGhiChuCachDung.Properties.DataSource = dmGhiChuCachDungBussiness.GetDmGhiChuCachDungList();
            lkGhiChuCachDung.Properties.ValueMember = "MaGhiChuCachDung";
            lkGhiChuCachDung.Properties.DisplayMember = "TenGhiChuCachDung";
            lkGhiChuCachDung.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDanhMuc()
        {
            LoadDmDonVi();
            LoadDmHangSx();
            LoadLoaiVatTu();
            LoadPhanLoaiVt();
            LoadDmCachDung();
            LoadDmDonViCachDung();
            LoadDmGhiChuCachDung();
        }
        #endregion

        private void frmcapnhatDMVatu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaVatTu, "Mã vật tư") == false) return;
            if (Validation.ValidLookUpEdit(lkDonVi, "Đơn vị") == false) return;
            if (Validation.ValidLookUpEdit(lkHangSanXuat, "Hãng sản xuất") == false) return;
            if (Validation.ValidLookUpEdit(lkLoaiVatTu, "Loại vật tư") == false) return;
            if (Validation.ValidLookUpEdit(lkPhanLoaiVatTu, "Phân loại vật tư") == false) return;
            //if (Validation.ValidLookUpEdit(lkCachDungThuoc, "Cách dùng") == false) return;
            if (Validation.ValidIsnumber(txtSoLanDungThuoc, "Số lần dùng") == false) { return; }
            if (Validation.ValidIsnumber(txtSoLuongDungThuoc, "Số lượng dùng") == false) { return; }
            //if (Validation.ValidLookUpEdit(lkDonViDungThuoc, "Đơn vị dùng") == false) return;
            //if (Validation.ValidLookUpEdit(lkGhiChuCachDung, "Ghi chú cách dùng") == false) return;

            objDmVatTuEntity = new DmVatTuEntity()
            {
                MaVatTu = txtMaVatTu.Text.Trim(),
                TenVatTu = txtTenVatTu.Text.Trim(),
                TenGoc = txtTenGoc.Text.Trim(),
                MaDonVi = lkDonVi.EditValue.ToString(),
                MaHangSanXuat = lkHangSanXuat.EditValue.ToString(),
                MaLoaiVatTu = lkLoaiVatTu.EditValue.ToString(),
                MaPhanLoaiVatTu = lkPhanLoaiVatTu.EditValue.ToString(),
                ThanhPhan = txtThanhPhan.Text.Trim(),
                HamLuong = txtHamLuong.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked,
                MaCachDung = lkCachDungThuoc.EditValue.ToString(),
                SoLuongDung = double.Parse(txtSoLuongDungThuoc.Text),
                LanDung = double.Parse(txtSoLanDungThuoc.Text),
                MaDonViDung = lkDonViDungThuoc.EditValue.ToString(),
                MaGhiChuCachDung = lkGhiChuCachDung.EditValue.ToString(),
                HuongDanSuDung = txtHuongDanSuDungThuoc.Text.Trim()
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmVatTuBusiness.GetDmVatTu(txtMaVatTu.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaVatTu, "Mã vật tư") == false) return;

                dmVatTuBusiness.InsertDmVatTu(objDmVatTuEntity);
                ClearData();
            }
            else
            {
                dmVatTuBusiness.EditDmVatTu(objDmVatTuEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmVatTuEvent != null)
                dmVatTuEvent(sender, e);
        }

        private void frmCapNhatDmVaTu_Load(object sender, EventArgs e)
        {

        }

        void ClearData()
        {
            txtMaVatTu.Text = string.Empty;
            txtTenVatTu.Text = string.Empty;
            txtTenGoc.Text = string.Empty;
            lkDonVi.EditValue = ConstantManagerClinic.sValueMember;
            lkHangSanXuat.EditValue = ConstantManagerClinic.sValueMember;
            lkLoaiVatTu.EditValue = ConstantManagerClinic.sValueMember;
            lkPhanLoaiVatTu.EditValue = ConstantManagerClinic.sValueMember;
            txtThanhPhan.Text = string.Empty;
            txtHamLuong.Text = string.Empty;
            lkCachDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
            txtSoLanDungThuoc.Text = "0";
            txtSoLuongDungThuoc.Text = "0";
            lkDonViDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
            lkGhiChuCachDung.EditValue = ConstantManagerClinic.sValueMember;
            txtHuongDanSuDungThuoc.Text = string.Empty;
            txtMaVatTu.Focus();
        }

        private void lkLoaiVatTu_EditValueChanged(object sender, EventArgs e)
        {
            LoadPhanLoaiVt();
        }

    }
}